package com.chcit.mobile.mvp.model;

import android.app.Application;

import com.alibaba.fastjson.JSONObject;
import com.chcit.mobile.app.contains.HttpMethodContains;
import com.chcit.mobile.mvp.model.api.service.UserService;
import com.chcit.mobile.mvp.model.entity.Locator;
import com.chcit.mobile.mvp.model.entity.Reason;
import com.chcit.mobile.mvp.model.entity.ReceiveBean;
import com.chcit.mobile.mvp.model.entity.ResultBean;
import com.google.gson.Gson;
import com.jess.arms.integration.IRepositoryManager;
import com.jess.arms.mvp.BaseModel;

import com.jess.arms.di.scope.FragmentScope;

import javax.inject.Inject;

import com.chcit.mobile.mvp.contract.ReceiveDetailContract;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.functions.Function;


@FragmentScope
public class ReceiveDetailModel extends BaseModel implements ReceiveDetailContract.Model {
    @Inject
    Gson mGson;
    @Inject
    Application mApplication;

    @Inject
    public ReceiveDetailModel(IRepositoryManager repositoryManager) {
        super(repositoryManager);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mGson = null;
        this.mApplication = null;
    }

    @Override
    public Observable<Locator> getLocatorId(JSONObject json) {
        return mRepositoryManager.obtainRetrofitService(UserService.class)
                .request(HttpMethodContains.LOCATORS,json)
                .map(jsonObject -> {
                    ResultBean resultBean = mGson.fromJson(jsonObject.toJSONString(),ResultBean.class);
                    if(resultBean.isSuccess()){
                        return resultBean.getRows(Locator.class).get(0);
                    }else{
                        throw new Exception("请求失败: "+resultBean.getMsg());
                    }


                });
    }

    @Override
    public Observable<ResultBean> doConfirm(JSONObject json) {
        return mRepositoryManager.obtainRetrofitService(UserService.class)
                .request(HttpMethodContains.CHECK_RECEIVE_OK,json)
                .map(jsonObject -> {
                    ResultBean resultBean = mGson.fromJson(jsonObject.toJSONString(),ResultBean.class);
                    if(resultBean.isSuccess()){
                        return resultBean;
                    }else{
                        throw new Exception("请求失败: "+resultBean.getMsg());
                    }

                });

    }

    @Override
    public Observable<List<Locator>> getLocators(JSONObject json) {
        return mRepositoryManager.obtainRetrofitService(UserService.class)
                .request(HttpMethodContains.LOCATORS,json)
                .map(jsonObject -> {
                    ResultBean resultBean = mGson.fromJson(jsonObject.toJSONString(),ResultBean.class);
                    if(resultBean.isSuccess()){
                        return resultBean.getRows(Locator.class);
                    }else{
                        throw new Exception("请求失败: "+resultBean.getMsg());
                    }

                });
    }

    @Override
    public Observable<List<Reason>> getReans(JSONObject json) {
       return mRepositoryManager.obtainRetrofitService(UserService.class)
                .request(HttpMethodContains.REJECT_REASON,json)
                .map(jsonObject -> {
                    ResultBean resultBean = mGson.fromJson(jsonObject.toJSONString(),ResultBean.class);
                    if(resultBean.isSuccess()){
                        return resultBean.getRows(Reason.class);
                    }else{
                        throw new Exception("请求失败: "+resultBean.getMsg());
                    }

                });
    }
}